package org.nothing.ordermaster.controller;

import lombok.extern.slf4j.Slf4j;
import org.nothing.ordermaster.converter.OrderForm2OrderDTOConverter;
import org.nothing.ordermaster.dto.OrderDTO;
import org.nothing.ordermaster.enums.ResultEnum;
import org.nothing.ordermaster.exception.SellException;
import org.nothing.ordermaster.form.OrderForm;
import org.nothing.ordermaster.service.BuyerService;
import org.nothing.ordermaster.service.OrderService;
import org.nothing.ordermaster.util.ResultVOUtil;
import org.nothing.ordermaster.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController
{
    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    // (1) 创建订单 POST /sell/buyer/order/create
    @PostMapping("/create")
    public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult)
    {
        // 实体类校验信息返回结果 错误处理
        if (bindingResult.hasErrors())
        {
            log.error("【创建订单】参数不正确, orderForm = {}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        // 将 OrderForm 转换成 OrderDTO
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);

        // 判断订单详情列表是否为空
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList()))
        {
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO createResult = orderService.create(orderDTO); // 创建订单
        Map<String, String> map = new HashMap<>();
        map.put("orderId", createResult.getOrderId()); // 设置 orderId
        return ResultVOUtil.success(map); // 返回订单号
    }

    // (2) 订单列表 GET /sell/buyer/order/list
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid, @RequestParam("page") Integer page, @RequestParam("size") Integer size)
    {
        if (!StringUtils.hasText(openid))
        {
            log.error("【查询订单列表】openid 为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        PageRequest pageRequest = PageRequest.of(page, size);
        // 查询订单列表
        Page<OrderDTO> orderDTOPage = orderService.findList(openid, pageRequest);

        return ResultVOUtil.success(orderDTOPage.getContent());
    }

    // (3) 订单详情 GET /sell/buyer/order/detail
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid, @RequestParam("orderId") String orderId)
    {
        OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);
        return ResultVOUtil.success(orderDTO);
    }

    // (4) 取消订单 POST /sell/buyer/order/cancel
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid, @RequestParam("orderId") String orderId)
    {
        OrderDTO orderDTO = buyerService.cancelOrder(openid, orderId);
        return ResultVOUtil.success();
    }
}
